<?php
include('setup.php');
$hostname_logon = 'localhost';      //Database server LOCATION
    $database_logon = 'wwwmsc_newcareer';       //Database NAME
    $username_logon = 'wwwmsc';       //Database USERNAME
    $password_logon = 'Slpnxhe}bX4h';       //Database PASSWORD
	$database_connection = mysql_connect($hostname_logon,$username_logon,$password_logon) or die("couldnt connect with mysql");
	$database_selection = mysql_select_db($database_logon,$database_connection);
	mysql_query( "SET NAMES utf8", $database_connection );
        mysql_query( "SET CHARACTER SET utf8", $database_selection );
// πέρνουμε τα πεδία από τη φόρμα μας και τα βάζουμε σε μεταβλητές
$user_level="select * from USERS where user_id=".$_POST['userid'];
$user_result=mysql_query($user_level);
$user_row=mysql_fetch_array($user_result);
$user_level=$user_row['user_level'];
$userid = $_POST["userid"];
$user_name = $_POST['user_name'];
$user_fname = $_POST['user_fname'];
$user_lname = $_POST['user_lname'];
$user_locid = $_POST['user_locid'];
$user_phone1 = $_POST['user_phone1'];
$user_phone2 = $_POST['user_phone2'];
$user_gender = $_POST['user_gender'];
$user_email = $_POST['user_email'];
$user_website = $_POST['user_website'];
$user_street_number = $_POST['street_number'];
$user_route = $_POST['route'];
$user_locality = $_POST['locality'];
$user_administrative_area_level_1 = $_POST['administrative_area_level_1'];
$user_postal_code = $_POST['postal_code'];
$user_autocomplete = $_POST['autocomplete'];
if($_REQUEST['old_address']!=$_REQUEST['autocomplete'] && $_REQUEST['autocomplete']!=""){
	$loc_country =$_POST["country"];
		$loc_locality =$_POST["locality"];
		$loc_route =$_POST["route"];
		$loc_postal_code =$_POST["postal_code"];
		$loc_administrative_area_level_1 =$_POST["administrative_area_level_1"];
		$loc_street_number =$_POST["street_number"];
	 $insert_location_query = "insert into `LOCATION` set
								`loc_country`	=	'$loc_country'
							,	`loc_locality`	=	'$loc_locality'
							,	`loc_route`	=	'$loc_route'
							,	`loc_postal_code`	=	'$loc_postal_code'
							,	`loc_administrative_area_level_1`	=	'$loc_administrative_area_level_1'
							,	`loc_street_number`	=	'$loc_street_number'	";
	$insert_location_result	=	mysql_query($insert_location_query) or die(mysql_error());
	$user_locid = mysql_insert_id();
	
}
	
	$age=$_REQUEST['userd_age'];
	$userd_catid=$_REQUEST['user_cat'];
	$userd_education=$_REQUEST['userd_education'];
	$old_file=$_REQUEST['old_file'];
	$logo_old_file=$_REQUEST['logo_old_file'];
	$comp_name=$_REQUEST['comp_name'];
	$comp_desc=$_REQUEST['comp_desc'];
	$comp_loc=$_REQUEST['comp_loc'];
   $cv="";
  $comp_logo="";
  
  if($user_level=="4")
  {
  
   if ($_FILES["cv"]["error"] > 0)
  {
  	
  }
		else
  {
  if($old_file!="" && file_exists($old_file))
  {
  unlink("../cv/".$old_file)or die("not found");
  }
  
	   if($_FILES["cv"]["type"]!="application/pdf" || $_FILES["cv"]["type"]!="application/pdf")
	   {
	 		  //header( "Location: ../portal.php?action=profile&file=1");	
	   }else{
			   $cv=date('dmhis').$_FILES['cv']['name'];
			   move_uploaded_file($_FILES['cv']['tmp_name'],"../cv/".$cv);
	   }
  }
  
  $select_user_detail="select * from USERS_DETAILS where userd_id=".$userid;
  $user_result=mysql_query($select_user_detail);
  $num_rows=mysql_num_rows($user_result);
  if($num_rows > 0)
  {
	  $update_user_detail="update  USERS_DETAILS set userd_age='".$age."',userd_catid='".$userd_catid."',userd_education='".$userd_education."'";
	  if($cv!="")
	  {
	  $update_user_detail.=" ,userd_cv='".$cv."'";
	  }
	$update_user_detail.=" where userd_id=".$userid;
	  mysql_query($update_user_detail)or die(mysql_error());
  }else{
  $insert_user_detail="insert into USERS_DETAILS set userd_age='".$age."',userd_catid='".$userd_catid."',userd_education='".$userd_education."',userd_cv='".$cv."', 	userd_id='".$userid."'";
   mysql_query($insert_user_detail)or die(mysql_error());
  
  }
  
  }
    if($user_level=="3")
  {
  
   if ($_FILES["comp_logo"]["error"] > 0)
  {
  	
  }
		else
  {
  if($logo_old_file!="" && file_exists($logo_old_file))
  {
  unlink("../logo/".$logo_old_file);
  }
	 
			 $comp_logo=date('dmhis').$_FILES['comp_logo']['name'];
			   move_uploaded_file($_FILES['comp_logo']['tmp_name'],"../logo/".$comp_logo);
	   
  }

 $select_user_detail="select * from COMPANIES_DETAILS where comp_id=".$userid;
  $user_result=mysql_query($select_user_detail);
  $num_rows=mysql_num_rows($user_result);
  if($num_rows > 0)
  {
	   $update_user_detail="update  COMPANIES_DETAILS set comp_name='".$comp_name."',comp_desc='".$comp_desc."'";
	  if($comp_logo!="")
	  {
	  $update_user_detail.=" ,comp_logo='".$comp_logo."'";
	  }
	 $update_user_detail.=" where comp_id=".$userid;
	  mysql_query($update_user_detail)or die(mysql_error());
  }else{
   $insert_user_detail="insert into COMPANIES_DETAILS set comp_name='".$comp_name."',comp_desc='".$comp_desc."',comp_logo='".$comp_logo."',comp_id='".$userid."'";
   mysql_query($insert_user_detail)or die(mysql_error());
  
  }
  
  }
   
   
// δημιουργία αντικειμένου setup
$setup = new setup();

// τρέχω το query μου για insert
$setup->qry("update USERS set 
user_name = '".$user_name."',
user_fname = '".$user_fname."',
user_lname = '".$user_lname."',
user_locid = '".$user_locid."',
user_phone1 = '".$user_phone1."',
user_phone2 = '".$user_phone2."',
user_gender = '".$user_gender."',
user_email = '".$user_email."',
user_website = '".$user_website."',
user_autocomplete = '".$user_autocomplete."' where user_id = '".$userid."'")or die(mysql_error());

// αν δεν με βγάλει κάποιο error τότε είναι επιτυχημένη η εγγραφή
if($error != "Error")
{
	header( "Location: ../portal.php?action=profile");	
}
else
{
	header( "Location: ../portal.php?action=profile");	
}

?>